package com.leo.springbootmultithreadbasic.lock.reentrantlock.fairandunfair;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Leo
 * @version V1.0
 * @date 2021/11/3 11:09 PM
 */
public class MyService {

    private Logger logger = LoggerFactory.getLogger(MyService.class);

    // 创建锁
    private ReentrantLock lock;

    public MyService(boolean fair) {
        lock = new ReentrantLock(fair);
    }


    /**
     * test get lock
     */
    public void testGetLock() {
        // get lock
        lock.lock();

        try {
            logger.info("ThreadName= {}", Thread.currentThread().getName());
        } finally {
            // release lock
            lock.unlock();
        }
    }
}
